index.tsx 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. import { useParams } from 'common'
  2. import { useRouter } from 'next/router'
  3. import { useEffect } from 'react'
  4. import { buildTableEditorUrl } from '@/components/grid/BrivenGrid.utils'
  5. import { SidePanelEditor } from '@/components/interfaces/TableGridEditor/SidePanelEditor/SidePanelEditor'
  6. import { DefaultLayout } from '@/components/layouts/DefaultLayout'
  7. import { EditorBaseLayout } from '@/components/layouts/editors/EditorBaseLayout'
  8. import { TableEditorLayout } from '@/components/layouts/TableEditorLayout/TableEditorLayout'
  9. import { TableEditorMenu } from '@/components/layouts/TableEditorLayout/TableEditorMenu'
  10. import { NewTab } from '@/components/layouts/Tabs/NewTab'
  11. import { useDashboardHistory } from '@/hooks/misc/useDashboardHistory'
  12. import { useQuerySchemaState } from '@/hooks/misc/useSchemaQueryState'
  13. import { editorEntityTypes, useTabsStateSnapshot } from '@/state/tabs'
  14. import type { NextPageWithLayout } from '@/types'
  15. const TableEditorPage: NextPageWithLayout = () => {
  16. const router = useRouter()
  17. const { ref: projectRef } = useParams()
  18. const tabStore = useTabsStateSnapshot()
  19. const { selectedSchema } = useQuerySchemaState()
  20. const { history, isHistoryLoaded } = useDashboardHistory()
  21. const onTableCreated = (table: { id: number }) => {
  22. router.push(
  23. `/project/${projectRef}/editor/${table.id}${!!selectedSchema ? `?schema=${selectedSchema}` : ''}`
  24. )
  25. }
  26. useEffect(() => {
  27. if (isHistoryLoaded && projectRef && router) {
  28. const lastOpenedTableId = Number(history.editor)
  29. const lastTabId = Number(
  30. tabStore.openTabs.find((id) => editorEntityTypes.table.includes(tabStore.tabsMap[id]?.type))
  31. )
  32. // Handle redirect to last opened table tab, or last table tab
  33. if (Number.isInteger(lastOpenedTableId)) {
  34. const lastOpenedTableData = tabStore.tabsMap[lastOpenedTableId]
  35. router.push(
  36. buildTableEditorUrl({
  37. projectRef,
  38. tableId: lastOpenedTableId,
  39. schema: lastOpenedTableData?.metadata?.schema,
  40. })
  41. )
  42. } else if (Number.isInteger(lastTabId)) {
  43. const lastOpenedTableData = tabStore.tabsMap[lastTabId]
  44. router.push(
  45. buildTableEditorUrl({
  46. projectRef,
  47. tableId: lastTabId,
  48. schema: lastOpenedTableData?.metadata?.schema,
  49. })
  50. )
  51. }
  52. }
  53. // eslint-disable-next-line react-hooks/exhaustive-deps
  54. }, [isHistoryLoaded, projectRef, router])
  55. return (
  56. <>
  57. <NewTab />
  58. <SidePanelEditor onTableCreated={onTableCreated} />
  59. </>
  60. )
  61. }
  62. TableEditorPage.getLayout = (page) => (
  63. <DefaultLayout>
  64. <EditorBaseLayout
  65. productMenu={<TableEditorMenu />}
  66. product="Table Editor"
  67. productMenuClassName="overflow-y-hidden"
  68. >
  69. <TableEditorLayout>{page}</TableEditorLayout>
  70. </EditorBaseLayout>
  71. </DefaultLayout>
  72. )
  73. export default TableEditorPage